<?php

namespace Registro\Frontend;

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Config\Adapter\Ini;

class Module implements ModuleDefinitionInterface {

    private $_config;

    public function __construct()
    {
        /**
         * Read configuration
         */
        $this->_config = new Ini( __DIR__ . '/config/config.ini' );
    }

    /**
     * Registers the module auto-loader
     */
    public function registerAutoloaders( \Phalcon\DiInterface $di = null )
    {

        $loader = new Loader();

        $loader->registerNamespaces(
                [
                    'Registro\Frontend\Controllers' => $this->_config->frontend->controllersDir,
                    'Registro\Frontend\Forms' => $this->_config->frontend->formsDir,
                ]
        )->register();
    }

    /**
     * Registers the module-only services
     *
     * @param Phalcon\DI $di
     */
    public function registerServices( \Phalcon\DiInterface $di )
    {
        $view = $di->getShared( 'view' );
        $view->setViewsDir( $this->_config->frontend->viewsDir );
        
        /**
         * Setting up the view component
         */
//        $di->set( 'view', function() use ( $di ){
//            $view = new View();
//            $view->setViewsDir( $this->_config->frontend->viewsDir )
//                    ->setLayoutsDir( $di->get( 'globalConfig' )->application->layoutDir )
//                    ->setTemplateAfter( 'main' )
//                    ->registerEngines( array(
//                        ".phtml" => 'Phalcon\Mvc\View\Engine\Volt'
//                    ) );
//            return $view;
//        } );
        /**
         * Setting up volt
         */
//        $di->set( 'volt', function($view, $di){
//
//            $volt = new \VoltEngine( $view, $di );
//
//            $volt->setOptions( array(
//                "compiledPath" => APP_PATH . "cache/volt/"
//            ) );
//
//            $compiler = $volt->getCompiler();
//            $compiler->addFunction( 'is_a', 'is_a' );
//
//            return $volt;
//        }, true );
    }

}
